package com.xbongbong.sso.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.sso.pojo.dto.OauthStepAddDTO;
import com.xbongbong.sso.pojo.dto.OauthStepDetailDTO;
import com.xbongbong.sso.pojo.dto.OauthStepListDTO;
import com.xbongbong.sso.pojo.dto.OauthStepUpdateDTO;
import com.xbongbong.sso.pojo.vo.OauthStepAddVO;
import com.xbongbong.sso.pojo.vo.OauthStepDetailVO;
import com.xbongbong.sso.pojo.vo.OauthStepListVO;
import com.xbongbong.sso.pojo.vo.OauthStepUpdateVO;
import com.xbongbong.sso.service.OauthStepService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 授权步骤
 * 创建时间 2020/12/28 2:51 PM
 * 修改时间 2020/12/28 2:51 PM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/oauthStep")
public class OauthStepController {

    private static final Logger LOG = LoggerFactory.getLogger(OauthStepController.class);

    @Resource
    private OauthStepService oauthStepService;

    /**
     * 授权步骤列表
     * @param oauthStepListDTO 关联DTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid OauthStepListDTO oauthStepListDTO, BindingResult br) throws Exception{
        XbbResponse<OauthStepListVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthStepListVO oauthStepListVO = oauthStepService.list(oauthStepListDTO);
                response = new XbbResponse<>(oauthStepListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权步骤详情
     * @param oauthStepDetailDTO 关联DTO
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid OauthStepDetailDTO oauthStepDetailDTO, BindingResult br) throws Exception{
        XbbResponse<OauthStepDetailVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthStepDetailVO oauthStepDetailVO = oauthStepService.detail(oauthStepDetailDTO);
                response = new XbbResponse<>(oauthStepDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权步骤新建
     * @param oauthStepAddDTO 关联DTO
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid OauthStepAddDTO oauthStepAddDTO, BindingResult br) throws Exception{
        XbbResponse<OauthStepAddVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthStepAddVO oauthStepAddVO = oauthStepService.add(oauthStepAddDTO);
                response = new XbbResponse<>(oauthStepAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权步骤编辑
     * @param oauthStepUpdateDTO 关联DTO
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid OauthStepUpdateDTO oauthStepUpdateDTO, BindingResult br) throws Exception{
        XbbResponse<OauthStepUpdateVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthStepUpdateVO oauthStepUpdateVO = oauthStepService.update(oauthStepUpdateDTO);
                response = new XbbResponse<>(oauthStepUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
